package flare.bean.coffee;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Type;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitScheduler;


public class Main extends JavaPlugin implements Listener {
	public final Logger logger = Logger.getLogger("Minecraft");
	public static Main plugin;

	@Override
	public void onEnable() {
		
		Bukkit.getPluginManager().registerEvents(this, this);
		ItemStack Flare = new ItemStack(Material.BLAZE_ROD);
		ItemMeta FlareMeta = Flare.getItemMeta();
		FlareMeta.setDisplayName(Color.RED + "Flare");
		Flare.setItemMeta(FlareMeta);
	}

	@Override
	public void onDisable() {
	}

	@EventHandler
	public void unicorn(PlayerInteractEvent evt) {
		if (evt.getAction() == Action.LEFT_CLICK_AIR
				| evt.getAction() == Action.LEFT_CLICK_BLOCK
				| evt.getAction() == Action.RIGHT_CLICK_AIR
				| evt.getAction() == Action.RIGHT_CLICK_BLOCK) {

			if (evt.getPlayer().getItemInHand() == new ItemStack(
					Material.BLAZE_ROD))
				;

		}
	}
         //
	@EventHandler
	public void derp(PlayerInteractEvent evt) {
		final Player p = evt.getPlayer();
		final Location loc = p.getLocation();
		if (p.getItemInHand().isSimilar(new ItemStack(Material.BLAZE_ROD))) {
			p.sendMessage(ChatColor.RED + "Flare Released!");
			p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD));
			p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1, 1));
			FireworkEffectPlayer fe = new FireworkEffectPlayer(); 
		    
			
			
			BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
			        scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
			            public void run() {
			            	FireworkEffectPlayer fe = new FireworkEffectPlayer(); 
			            	try {
			    				fe.playFirework(p.getWorld(), loc, FireworkEffect.builder().withColor(Color.RED).with(Type.BALL_LARGE).build());
			    				p.playSound(p.getLocation(), Sound.GHAST_DEATH, 1,1);   //is it not the same way as the explsionS?
			    				for(int i = 0; i < 20; i++){
			    				fe.playFirework(p.getWorld(),new Location(p.getWorld() ,p.getLocation().getX(),p.getLocation().getY()+ i, p.getLocation().getZ()), FireworkEffect.builder().withColor(Color.RED).with(Type.BALL_LARGE).build());
			    				
			    				};
			            	}
			    				 catch (IllegalArgumentException e) {
			    				// TODO Auto-generated catch block
			    				e.printStackTrace();
			    			} catch (Exception e) {
			    				// TODO Auto-generated catch block
			    				e.printStackTrace();
			    				
			    					 }
			    			}
			    			
			    		}
			    	
			     			            
			        ,20L , 20L);
			    }
			
	
}
}
			